﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TradeSea;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TradeSea
{
    public class Shop
    {
        public MainGame Game;
        private SpriteBatch Batch;
        public TSContentManager Manager;

        private Dictionary<Keys, bool> PressedKeys = new Dictionary<Keys, bool>();

        public Dictionary<CommandAttribute, MethodInfo> Commands = new Dictionary<CommandAttribute, MethodInfo>();

        private List<Keys> NewKeys = new List<Keys>();

        public string currentInput = "";

        public bool ConsoleActive = true;
        List<ConsoleLine> OutputHistory = new List<ConsoleLine>();

        Inventory inventory;
        bool isBuying = false;
        int value = 2;

        public Shop(MainGame game, SpriteBatch batch, TSContentManager manager, Inventory playerInventory)
        {
            this.Game = game;
            this.Batch = batch;
            this.Manager = manager;
            inventory = playerInventory;
        }

        public bool openShop = false;

        public void sendtoserver()
        {
            TradeSeaNetClient.Singilton.CreateMessage(3);

            TradeSeaNetClient.Singilton.SendMessage(null, null);
        }

        public void UpdateShop()
        {
            KeyboardState state = Keyboard.GetState();

            List<Keys> pressedKeys = new List<Keys>(state.GetPressedKeys());

            NewKeys.Clear();

            var keys = PressedKeys.Keys;


            for (int q = 0; q < keys.Count; q++)
            {
                if (!pressedKeys.Contains(keys.ElementAt<Keys>(q)))
                {
                    PressedKeys[keys.ElementAt<Keys>(q)] = false;
                }
            }

            foreach (Keys item in pressedKeys)
            {
                if (PressedKeys.ContainsKey(item) && PressedKeys[item])
                {
                    continue;
                }
                else
                {
                    if (!PressedKeys.ContainsKey(item))
                    {
                        PressedKeys.Add(item, true);
                    }
                    else
                    {
                        PressedKeys[item] = true;
                    }

                    NewKeys.Add(item);
                }
            }

            if (NewKeys.Contains(Keys.F11))
            {
                openShop = false;
            }           

            foreach (Keys item in NewKeys)
            {
                if (GetFromKey(item) != '-')
                {
                    currentInput += GetFromKey(item);
                }

                if (item == Keys.Enter)
                {
                    int i = 0;
                    if (currentInput == "leave")
                    {
                        openShop = false;
                        OutputHistory = null;
                    }
                    else if (currentInput == "buy") openBuy();
                    else if (currentInput == "sell") openSell();
                    else if (isBuying == false)
                    {
                        while (inventory.getInventoryList().Count() > i)
                        {
                            if (currentInput.Equals(OutputHistory[i].Line))
                            {

                                    Print("Item sold for " + value + " gold", Color.HotPink);
                                    inventory.addMoney(value);
                                    inventory.removeItem(currentInput);
                                
                            }
                            i++;
                        }
                    }
                    else if (isBuying == true && inventory.getBalance() >= value)
                    {
                        while (OutputHistory.Count() > i)
                        {
                            if (currentInput.Equals(OutputHistory[i].Line) && inventory.getBalance() >= value)
                            {
                                Print("Item purchased for " + value + " gold", Color.HotPink);
                                inventory.takeMoney(value);
                                inventory.addToInventory(currentInput);
                            }
                            else if (currentInput.Equals(OutputHistory[i].Line) && inventory.getBalance() < value)
                            {
                                Print("You do not have enough gold for this item", Color.DarkRed);
                                break;
                            }
                            i++;
                        }
                    }
                    else
                    {
                        Print("Enter a item to buy or sell", Color.DarkRed);
                    }

                    currentInput = "";
                }

                if (item == Keys.Back)
                {
                    if (currentInput.Length > 0)
                    {
                        currentInput = currentInput.Substring(0, currentInput.Length - 1);
                    }
                }
            }
        }

        //print all items you can buy here
        public void openBuy()
        {
            isBuying = true;
            if (OutputHistory != null) OutputHistory.Clear();
            Print("cannon", Color.Green);
            Print("apple", Color.Green);
            Print("slave", Color.Green);
            Print("rum", Color.Green);
            Print("fresh water", Color.Green);
            Print("good net", Color.Green);
            Print("super net", Color.Green);
        }
        //print inventory here
        public void openSell()
        {
            isBuying = false;
            if (OutputHistory != null) OutputHistory.Clear();
            int i = 0;
            while (inventory.getInventoryList().Count() > i)
            {
                Print(inventory.getInventoryList()[i].Item, Color.Red);
                i++;
            }
        }

        public void Print(string message, Color col)
        {
            this.OutputHistory.Add(new ConsoleLine(message, col));
        }

        char GetFromKey(Keys key)
        {
            switch (key)
            {
                case Keys.A:
                    return 'a';
                case Keys.B:
                    return 'b';
                case Keys.C:
                    return 'c';
                case Keys.D:
                    return 'd';
                case Keys.D0:
                    return '0';
                case Keys.D1:
                    return '1';
                case Keys.D2:
                    return '2';
                case Keys.D3:
                    return '3';
                case Keys.D4:
                    return '4';
                case Keys.D5:
                    return '5';
                case Keys.D6:
                    return '6';
                case Keys.D7:
                    return '7';
                case Keys.D8:
                    return '8';
                case Keys.D9:
                    return '9';
                case Keys.E:
                    return 'e';
                case Keys.F:
                    return 'f';
                case Keys.G:
                    return 'g';
                case Keys.H:
                    return 'h';
                case Keys.I:
                    return 'i';
                case Keys.J:
                    return 'j';
                case Keys.K:
                    return 'k';
                case Keys.L:
                    return 'l';
                case Keys.M:
                    return 'm';
                case Keys.N:
                    return 'n';
                case Keys.O:
                    return 'o';
                case Keys.P:
                    return 'p';
                case Keys.Q:
                    return 'q';
                case Keys.R:
                    return 'r';
                case Keys.S:
                    return 's';
                case Keys.Space:
                    return ' ';
                case Keys.T:
                    return 't';
                case Keys.U:
                    return 'u';
                case Keys.V:
                    return 'v';
                case Keys.W:
                    return 'w';
                case Keys.X:
                    return 'x';
                case Keys.Y:
                    return 'y';
                case Keys.Z:
                    return 'z';
                default:
                    return '-';
            }
        } // yay that's done


        public void drawShop()
        {
            if (openShop)
            {
                if (!this.ConsoleActive) return;

                Manager.FillRectange(this.Batch, new Rectangle(10, 10, Game.Window.ClientBounds.Width - 20, Game.Window.ClientBounds.Height - 20),
                    new Color(40, 40, 40, 255));

                int i = 0;

                if (OutputHistory != null)
                {
                    foreach (ConsoleLine item in this.OutputHistory)
                    {
                        this.Batch.DrawString(this.Manager.ConsoleFont, item.Line, new Vector2(15, i * 20 + 20), item.Col);
                        i++;
                    }
                }

                string input = "> " + currentInput;
                if (DateTime.Now.Millisecond % 75 == 0)
                {
                    input += "|";
                }

                this.Batch.DrawString(this.Manager.ConsoleFont, "Avast matey! Welcome to Ker Karraje island shop.", new Vector2(15, i * 20 + 20), Color.White);
                this.Batch.DrawString(this.Manager.ConsoleFont, "Arrrr you looking to buy, sell or leave?", new Vector2(15, (i + 1) * 20 + 20), Color.White);
                this.Batch.DrawString(this.Manager.ConsoleFont, "You have " + inventory.getBalance() + " gold", new Vector2(15, (i + 2) * 20 + 20), Color.White);


                this.Batch.DrawString(this.Manager.ConsoleFont, input, new Vector2(20, Game.Window.ClientBounds.Height - 40), Color.LightGray);
            }
        }
    }
}
